/**
 * 
 * @author Mihail A. Muhin
 */
package interfaces.clients.storage;

import sun.misc.Compare;
import sun.misc.Sort;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * File repository without any diffs.
 *
 * @author Chikov Ivan
 */
public class NoDiffsRepository implements Repository {
	private static NoDiffsRepository ourInstance = new NoDiffsRepository();

	private static final String repositoryLocation = "c:\\Temp\\rrepos";

	private ExtendedFile repos = null;

	public static NoDiffsRepository getInstance() {
		return ourInstance;
	}

	private NoDiffsRepository() {
		repos = new ExtendedFile(repositoryLocation);
	}


	public String getFile(String filename, int version) throws StorageFileNotFoundException, StorageIOException {
		ExtendedFile file = new ExtendedFile(repos, filename);
		file = new ExtendedFile(file, Integer.toString(version));
		return file.read();
	}


	public void putFile(String filename, String contents) throws StorageIOException, StorageFileNotFoundException {
		ExtendedFile file = new ExtendedFile(repos, filename);

		/*Making new version. If file doesn't exists then it would be created with version 1
		which is provided by method getLastVersion returning 0 for not existing files*/
		file = new ExtendedFile(file, Integer.toString(getLastVersion(filename)+1));
		/*If file doesn't exist directories where its versions would be stored
				should be created*/
		file.createFile();
		/*Writing contents to the file*/
		file.write(contents);
	}

	public int getLastVersion(String filename) {
		File file = new File(repos, filename);
		if(!file.exists()) {
			return 0;
		} else {
			return Integer.valueOf(getSortedNumericalFiles(file)[getSortedFiles(file).length-1]).intValue();
		}
	}

	public List getFiles() {
		ArrayList al = new ArrayList();
		//String [] names = repos.list(); //Unsorted
		String names[] = getSortedFiles(repos);
		for(int i = 0; i < names.length; i++) al.add(names[i]);
		return al;
	}

	public boolean clearRepository() throws StorageIOException, StorageFileNotFoundException {
		return repos.emptyDirectory();
	}

	public MutableTreeNode getFilesTree() {
		return getFilesTree(repos);
	}

	private MutableTreeNode getFilesTree(File dir) {
		DefaultMutableTreeNode top = new DefaultMutableTreeNode(dir.getName(), true);
		File l[] = dir.listFiles();
		for(int i = 0; i < l.length; i++) {
			if(l[i].isDirectory()) {
				top.add(getFilesTree(l[i]));
			}
			//TODO: make something better here
			//else top.add(new DefaultMutableTreeNode(l[i].getName(), false));
			else {
				top.setAllowsChildren(false);
				break;
			}
		}
		return top;
	}

	private String[] getSortedFiles(File file) {
		String[] files = file.list();
		Sort.quicksort(files, new Compare() {
			public int doCompare(Object object, Object object1) {
				return ((String) object).compareTo((String) object1);
			}
		});
		return files;
	}

	private String[] getSortedNumericalFiles(File file) {
		String[] files = file.list();
		Sort.quicksort(files, new Compare() {
			public int doCompare(Object object, Object object1) {
				return Integer.parseInt((String) object)-Integer.parseInt((String) object1);
			}
		});
		return files;
	}


}